<?php
namespace Home\Controller;

use Think\Controller;

class ArticleController extends HomeController
{

    public function index()
    {
        // 承接 导航栏 , 判定登录状态 
        if (empty(session('home'))) {
            $this->display('Login/index');
            exit;
        }

        // 判定页码以及 0级话题的id传参
        // 判断分页条件
        if(!empty($_GET['p']) && intval($_GET['p']) > 0) {
            $p = intval($_GET['p']);
        } else {
            $p = 1;
        }

        // 查询条件
        $ar = M('article');
        $data = $ar->field('ar.id arid, ar.filename arfile, ar.title artitle, ar.image arimage, ar.datetime artime, u.id uid, u.image uimage, u.name uname')->table('zhihu_article ar, zhihu_user u')->where('ar.user_id=u.id AND ar.display=1')->order('artime DESC')->page($p, 10)->select();

        for ($i=0; $i < count($data); $i++) {
            //拼接路径
            $path = './Data/'.substr($data[$i]['arfile'], 0, 4).'/'.substr($data[$i]['arfile'], 4, 2).'/'.substr($data[$i]['arfile'], 6, 2).'/'.$data[$i]['arfile'];
            // $data[$i]['part'] = html_entity_decode(file_get_contents($path));
            $data[$i]['part'] = mb_substr(strip_tags(html_entity_decode(file_get_contents($path, null, null, 0, 900))), 0, 240) ;
        }

        // 页面需求数据
        $this->assign('title', '文章 - 知乎');
        $this->assign('p', $p+1);
        $this->assign('data', $data);
        $this->display('Article/index');
    }



    public function show()
    {
        if(!empty($_GET['arid']) && intval($_GET['arid']) > 0) {
            $arid = intval($_GET['arid']);
        } else {
            $this->error('无效的arid...');
            exit;
        }

        $ar = M('article');
        $data = $ar->field('ar.id arid, ar.filename arfile, ar.title artitle, ar.image arimage, ar.discuss_number ardnum,ar.datetime artime, u.id uid, u.image uimage, u.name uname')->table('zhihu_article ar, zhihu_user u')->where('ar.id='.$arid.' AND ar.user_id=u.id AND ar.display=1')->select();

        for ($i=0; $i < count($data); $i++) {
            //拼接路径
            $path = './Data/'.substr($data[$i]['arfile'], 0, 4).'/'.substr($data[$i]['arfile'], 4, 2).'/'.substr($data[$i]['arfile'], 6, 2).'/'.$data[$i]['arfile'];
            $data[$i]['content'] = html_entity_decode(file_get_contents($path));
            $data[$i]['acontent'] = preg_replace('/width=".*?"/', 'width="100%"', $data[$i]['acontent']);
            $data[$i]['content'] = str_replace('alt=""', 'width="100%"', $data[$i]['content']);
        }


        // 评论分页信息
        if (!empty($_GET['p']) && intval($_GET['p']) > 0) {
            $p = intval($_GET['p']);
        }else{
            $p = 1;
        }

        //查询文章评论
        $ad = M('article_discuss');
        $where = "ad.article_id={$arid} AND ad.user_id = u.id AND ad.display=1";
        $list = $ad->table('zhihu_article_discuss ad, zhihu_user u')->field('ad.id adid, ad.content adcontent, ad.user_id uid, ad.to_user_id tuid, ad.zan adzan, ad.datetime adtime, u.name uname, u.image uimg')->where($where)->order('ad.datetime DESC')->page($p, 20)->select();// 每页20条

        $u = M('user');
        $i = 0;
        $arr = array();
        foreach ($list as $k => $v) {
            $arr = $u->field('name, image')->where('id='.$v['tuid'])->select();
            $list[$i]['tuname'] = $arr['0']['name'];
            $list[$i]['tuimg'] = $arr['0']['image'];
            $i++;
        }

        // var_dump($data);
        // V($list);
        $this->assign('p', $p+1);
        $this->assign('list', $list);
        $this->assign('arid', $arid);
        $this->assign('data', $data);
        $this->assign('title', '文章 - 知乎');
        $this->display('Article/show');

    }


    // 举报 事件ajax 返回
    public function reportAr()
    {
        if (!empty($_GET['arid']) && intval($_GET['arid']) > 0) {
            $id = intval($_GET['arid']);
            $a = M('article');
            if($a->where('id='.$id)->setField('report', 1)){
                echo 1;
            }
        } else {
            echo 0;
        }
    }


    // 评论提交事件 ajax返回
    public function addDiscuss(){
        
        // 获取数据写入数据库
        if (!empty($_GET['arid']) && intval($_GET['arid']) > 0) {
            $data['article_id'] = intval($_GET['arid']);
            $arid = $data['article_id'];
        }else{
            echo '错误的文章id...';
            exit;
        }

        if (!empty($_GET['content'])) {
            $data['content'] = I('get.content');
        }else{
            echo '评论内容不能为空...';
            exit;
        }

        $data['user_id'] = $_SESSION['home']['id'];
        $data['datetime'] = date('Y-m-d H:i:s');

        // 评论写入 并增加文章表评论数量
        $ar = M('article');
        $ad = M('article_discuss');
        if ($ad->data($data)->add()) {
            $ar->where('id='.$data['article_id'])->setInc('discuss_number', 1);
        } else {
            echo '评论失败...';
        }

        // 评论分页信息
        if (!empty($_GET['p']) && intval($_GET['p']) > 0) {
            $p = intval($_GET['p']);
        }else{
            $p = 1;
        }

        //查询文章评论
        $ad = M('article_discuss');
        $where = "ad.article_id={$arid} AND ad.user_id = u.id AND ad.display=1";
        $list = $ad->table('zhihu_article_discuss ad, zhihu_user u')->field('ad.id adid, ad.content adcontent, ad.user_id uid, ad.to_user_id tuid, ad.zan adzan, ad.datetime adtime, u.name uname, u.image uimg')->where($where)->order('ad.datetime DESC')->page($p, 20)->select();// 每页20条

        $i = 0;
        $arr = array();
        foreach ($list as $k => $v) {
            $arr = M('user')->field('name, image')->where('id='.$v['tuid'])->select();
            $list[$i]['tuname'] = $arr['0']['name'];
            $list[$i]['tuimg'] = $arr['0']['image'];
            $i++;
        }

        // V($list);
        $this->assign('p', $p+1);
        $this->assign('list', $list);
        $this->assign('arid', $arid);
        $this->display('Article/ardis'); //返回一个页面
    }


    // 评论回复页面 ajax返回
    public function addReply()
    {
        // 获取数据写入数据库
        if (!empty($_GET['arid']) && intval($_GET['arid']) > 0) {
            $data['article_id'] = intval($_GET['arid']);
            $arid = $data['article_id'];
        }else{
            echo '错误的文章id...';
            exit;
        }

        // 获取数据写入数据库
        if (!empty($_GET['tuid']) && intval($_GET['tuid']) > 0) {
            $data['to_user_id'] = intval($_GET['tuid']);
        }else{
            echo '错误的回复用户id...';
            exit;
        }

        if (!empty($_GET['content'])) {
            $data['content'] = I('get.content');
        }else{
            echo '评论内容不能为空...';
            exit;
        }

        $data['user_id'] = $_SESSION['home']['id'];
        $data['datetime'] = date('Y-m-d H:i:s');

        // 评论写入 并增加文章表评论数量
        $ar = M('article');
        $ad = M('article_discuss');
        if ($ad->data($data)->add()) {
            $ar->where('id='.$data['article_id'])->setInc('discuss_number', 1);
        } else {
            echo '评论失败...';
        }

        // 评论分页信息
        if (!empty($_GET['p']) && intval($_GET['p']) > 0) {
            $p = intval($_GET['p']);
        }else{
            $p = 1;
        }

        //查询文章评论
        $ad = M('article_discuss');
        $where = "ad.article_id={$arid} AND ad.user_id = u.id AND ad.display=1";
        $list = $ad->table('zhihu_article_discuss ad, zhihu_user u')->field('ad.id adid, ad.content adcontent, ad.user_id uid, ad.to_user_id tuid, ad.zan adzan, ad.datetime adtime, u.name uname, u.image uimg')->where($where)->order('ad.datetime DESC')->page($p, 20)->select();// 每页20条

        $i = 0;
        $arr = array();
        foreach ($list as $k => $v) {
            $arr = M('user')->field('name, image')->where('id='.$v['tuid'])->select();
            $list[$i]['tuname'] = $arr['0']['name'];
            $list[$i]['tuimg'] = $arr['0']['image'];
            $i++;
        }

        // V($list);
        $this->assign('p', $p+1);
        $this->assign('list', $list);
        $this->assign('arid', $arid);
        $this->display('Article/ardis'); //返回一个页面

    }

    // 举报评论
    public function reportAd()
    {
        if (!empty($_GET['adid']) && intval($_GET['adid']) > 0) {
            $id = intval($_GET['adid']);
            $ad = M('article_discuss');
            if($ad->where('id='.$id)->setField('report', 1)){
                echo 1;
            }
        } else {
            echo 0;
        }
    }


    // 翻页 ajax 返回
    public function showMore()
    {
        // 获取数据
        if (!empty($_GET['arid']) && intval($_GET['arid']) > 0) {
            $arid = intval($_GET['arid']);
        }else{
            echo '错误的文章id...';
            exit;
        }

        // 评论分页信息
        if (!empty($_GET['p']) && intval($_GET['p']) > 0) {
            $p = intval($_GET['p']);
        }else{
            $p = 1;
        }

        //查询文章评论
        $ad = M('article_discuss');
        $where = "ad.article_id={$arid} AND ad.user_id = u.id AND ad.display=1";
        $list = $ad->table('zhihu_article_discuss ad, zhihu_user u')->field('ad.id adid, ad.content adcontent, ad.user_id uid, ad.to_user_id tuid, ad.zan adzan, ad.datetime adtime, u.name uname, u.image uimg')->where($where)->order('ad.datetime DESC')->page($p, 20)->select();// 每页20条

        $i = 0;
        $arr = array();
        foreach ($list as $k => $v) {
            $arr = M('user')->field('name, image')->where('id='.$v['tuid'])->select();
            $list[$i]['tuname'] = $arr['0']['name'];
            $list[$i]['tuimg'] = $arr['0']['image'];
            $i++;
        }

        $this->assign('p', $p+1);
        $this->assign('list', $list);
        $this->assign('arid', $arid);
        $this->display('Article/ardis'); //返回一个页面

    }




}